﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using BookSystem.CommomHelper;
using BookSystem.Model;


namespace BookSystem.User
{
    public partial class ReturnBook : Form
    {
        public ReturnBook()
        {
            InitializeComponent();
        }

        private void ReturnBook_Load(object sender, EventArgs e)
        {
            loadData();
            getData();
        }




        //加载数据
        private void loadData()
        {

            bookId.Items.Clear();

            List<model.Brrow> brs = BLL.Borrow.getBorrowByUid("5");

            //遍历借阅数据，添加到combobox里。
            foreach (model.Brrow br in brs)
            {

                bookId.Items.Add(br.Bid);

            }

            //默认选择第1个item，当没有items时候，进行异常捕获
            try
            {

                bookId.SelectedIndex = 0;

            }
            catch (ArgumentOutOfRangeException e)
            {

                MessageBox.Show("当前没有需要归还的书...");


            }





        }

        //通过bookId_combobox来查询数据
        private void getData()
        {
            if (bookId.Text != String.Empty)
            {
                model.Brrow br = BLL.Borrow.getBorrowByBid(bookId.SelectedItem.ToString());

                if (br.Bid != null)
                {

                    model.Book book = BLL.Book.getBook(br.Bid);
                    model.User user = BLL.User.getUser(br.Uid);
                    bookName.Text = book.Bname;
                    userName.Text = user.Uname;

                    //时间计算
                    DateTime time = Convert.ToDateTime(br.date);
                    int between = DateTime.Now.DayOfYear - time.DayOfYear;

                    //金额计算
                    if (between > 60)
                    {
                        compensation.Text = Convert.ToString((between - 60) / 10 + "￥");

                    }
                    else
                    {

                        compensation.Text = "0￥";

                    }


                }
                else
                {
                    bookId.Text = String.Empty;
                    MessageBox.Show("没有被借阅的书");
                    return;

                }



            }


        }

        //回车键事件
        private void bookId_KeyPress(object sender, KeyPressEventArgs e)
        {

            //按下回车键触发
            if (e.KeyChar == 13)
            {

                getData();

            }
        }

        //下拉选项更改事件
        private void bookId_SelectedIndexChanged(object sender, EventArgs e)
        {
            getData();
        }

        //归还按钮事件
        private void returnButton_Click(object sender, EventArgs e)
        {
            if (bookId.Text != String.Empty)
            {

                if (BLL.Borrow.delBorrow(bookId.Text))
                {
                    bookId.Text = String.Empty;
                    MessageBox.Show("图书已归还！");
                    bookName.Clear();
                    userName.Clear();
                    compensation.Clear();
                    loadData();
                    getData();

                }
                else
                {

                    MessageBox.Show("归还失败，请重试！！");

                }

            }
            else
            {

                MessageBox.Show("请重新检查！");

            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }



    }
}
